<?php
/**
 * Created by PhpStorm.
 * User: 陈立强
 * Date: 2019/9/9
 * Time: 9:23
 */
/**
 * 商家模型
 * @author maniac
 */
namespace app\common\model;

use think\facade\Cache;
use think\Db;
use app\common\model\GoodCategory AS GoodCategoryModel;
class Stores extends SoftDel
{
    public $tableName = '商家';          // 数据表名称
    public $logId     = ['id', 'name'];     // 日志操作相关标识

    protected $autoWriteTimestamp = true;   // 自动写入时间戳
   

    public function getHjAttr($value, $data)
    {
        $album =explode(',',$data['hj_pic']);
        $arr=array();
        foreach ($album as $k => $v) {
            $arr[$k]['id'] = $v;
            $arr[$k]['path'] = attach_uri($v);
        }
        return $arr;
    }

    /**
     * 大类获取器
     * @param  string $value 字段值
     * @param  array  $data  所有数据
     */
    public function getCategoryNameAttr($value, $data)
    {
        $category = ShopType::field('name')->where('id', $data['category_id'])->find();
        return $category['name'];
    }



    

    /**
     * 身份证获取器
     * @param string $value 字段值
     * @param array $data 所有数据
     */
    public function getIdcardPicAttr($value, $data)
    {
        $album =explode(',',$data['idcard_id']);
        $arr=array();
        foreach ($album as $k => $v) {
            $arr[$k]['id'] = $v;
            $arr[$k]['path'] = attach_uri($v);
        }
        return $arr;
    }
    
    /**
     * 营业执照获取图片地址
     * @param  string $value 字段值
     * @param  array  $data  所有数据
     */
    public function getPicsAttr($value, $data)
    {
        return $data['pic'] ? attach_uri($data['pic']) : '';
    }

    /**
     * 商家banner获取器
     * @param string $value 字段值
     * @param array $data 所有数据
     */
    public function getPhotosPicsAttr($value, $data)
    {
        if(!empty($data['photos'])){
            $album =explode(',',$data['photos']);
            $arr=array();
            foreach ($album as $k => $v) {
                $arr[$k]['id'] =$v;
                $arr[$k]['path'] = attach_uri($v);
            }
           // p($arr);die;
            return $arr;
        }
        return '';

    }

    /**
     * 商家banner获取器
     * @param string $value 字段值
     * @param array $data 所有数据
     */
    public function getHjPicStrAttr($value, $data)
    {
        if(!empty($data['hj_pic'])){
            $album =explode(',',$data['hj_pic']);
            $arr=array();
            foreach ($album as $k => $v) {
                $arr[$k]['id'] =$v;
                $arr[$k]['path'] = attach_uri($v);
            }
            // p($arr);die;
            return $arr;
        }
        return '';

    }

    /**
     * 行业二类获取器
     * @param string $value 字段值
     * @param array $data 所有数据
     */
    public function getTypeNameAttr($value, $data)
    {
        $album = explode(',',$data['type_id']);
        $arr=array();
        foreach ($album as $k => $v) {
            $arr[] = ShopType::field('name')->where('id', $v)->value('name');
        }
        return $arr;
    }

    /**
     * 省获取器
     * @param  string $value 字段值
     * @param  array  $data  所有数据
     */
    public function getRegionAttr($value, $data)
    {
        $temp['province_txt'] = Zoning::field('region_name')->where('region_id', $data['province'])->value('region_name');
        $temp['city_txt'] = Zoning::field('region_name')->where('region_id', $data['city'])->value('region_name');
        $temp['district_txt'] = Zoning::field('region_name')->where('region_id', $data['district'])->value('region_name');
        return $temp;
    }


    /**
     * 获取用户名
     */
    public function getUsernameAttr($key,$val){

        return Members::where('id',$val['uid'])->value('nickname');
    }

    /**
     * 获取用户名
     */
    public function getStatusTxtAttr($key,$val){

        $txtarr=['审核中','审核成功','拒绝审核'];
        return $txtarr[$val['status']];
    }

    /**
     * 获取分店数量
     */
    public function getSCountAttr($key,$val){

        $where=[
            ['parent_id','eq',$val['id']],
            ['status','eq',1]
        ];
        return Stores::where($where)->count("*");
    }

    /**
     * 商家图标获取图片地址
     * @param  string $value 字段值
     * @param  array  $data  所有数据
     */
    public function getImageUriStrAttr($value, $data)
    {
        return $data['image_uri'] ? attach_uri($data['image_uri']) : '';
    }
}
